<template>
  <view>
    <view class="goods-item" @click="commitDetail">
      <!-- 商品左侧图片区域 -->
      <view class="goods-item-left">
        <radio v-if="showRadio" @click="radioChangeHandler(goods)" :checked="goods.goods_state" color="#c00000" id="radio"></radio>
        <img :src="goods.goods_small_logo || defaultPic" class="goods-pic" alt="">
      </view>
      <!-- 商品右侧信息区域 -->
      <view class="goods-item-right">
        <!-- 商品标题 -->
        <view class="goods-name">
          {{goods.goods_name}}
        </view>
        <view class="goods-info-box">
          <!-- 商品价格 -->
          <view class="goods-price">
            ￥{{goods.goods_price | toFixed}}
          </view>
          <!-- 商品数量 -->
          <uni-number-box v-if="showNum" :min="1" :value="goods.goods_count" @change="changeValue()" id="number"/>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    name: "my-goods",
    // 商品信息对象
    props: {
      goods: {
        type: Object,
        default: {}
      },
      // 是否展示图片左侧的radio
      showRadio: {
        type: Boolean,
        // 默认情况下,不会展示radio组件
        default: false
      },
      // 是否展示价格右侧的NumberBox组件
      showNum: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        // 默认的图片
        defaultPic: 'https://img2.baidu.com/it/u=1866040383,1954286585&fm=253&fmt=auto&app=138&f=JPG?w=654&h=380'
      };
    },
    filters: {
      // 把数据处理为带两位小数点的数字
      toFixed(num){
        return Number(num).toFixed(2)
      }
    },
    methods: {
      // radio组件点击事件处理函数
      radioChangeHandler(goods){
        // 通过this.$emit()触发外界通过@绑定的radio-change事件
        // 同时把商品的id和勾选状态作为参数传递给radio-change事件处理函数
        this.$emit('radioChange',{
          // 商品的id
          goods_id: goods.goods_id,
          // 商品最新的勾选状态
          goods_state: !goods.goods_state
          })
      },
      
      // 商品的数量发生了变化
      changeValue(e){
        const goods_id = this.goods.goods_id
        this.$emit('countChange',{e,goods_id})
      },
      
      // 提交跳转商品详情页
      commitDetail(e){
        if(e.target.id != 'radio' && e.target.id != 'number'){
          this.$emit('goToDetail',this.goods.goods_id)
          // console.log(this.goods)
        }
      }
    }
  }
</script>

<style lang="scss">
  .goods-item {
    display: flex;
    padding: 10px 5px;
    border-bottom: 1px solid #f0f0f0;

    .goods-item-left {
      margin-right: 5px;
      display: flex;
      justify-content: space-between;
      align-items: center;

      .goods-pic {
        width: 100px;
        height: 100px;
        display: block;
      }
    }

    .goods-item-right {
      display: flex;
      flex: 1;
      flex-direction: column;
      justify-content: space-between;

      .goods-name {
        font-size: 13px
      }

      .goods-info-box{
        display: flex;
        justify-content: space-between;
        align-items: center;
        
        .goods-price {
          color: #c00000;
        }
      }
    }
  }
</style>
